package study.dq.jms.filter;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/11/22 16:17
 */
public class Outposts {

    public QueueSession session;
    public QueueSender sender;

    public Outposts() {
        try {
            ConnectionFactory factory = new ActiveMQConnectionFactory("admin","admin","tcp://127.0.0.1:61616");
            QueueConnection connection = ((ActiveMQConnectionFactory) factory).createQueueConnection();
            session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("Combat_Intelligence");
            sender = session.createSender(queue);
            connection.start();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        Outposts outposts = new Outposts();
        while (true){
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                Integer order;
                try {
                    String receive = reader.readLine();
                    order = Integer.valueOf(receive);
                    if (order>3 || order<1){
                        System.out.println("无效指令");
                        continue;
                    }
                } catch (NumberFormatException | IOException e) {
                    System.out.println("无效指令");
                    continue;
                }
                System.out.println("收到战备指令");
                TextMessage message = outposts.session.createTextMessage();
                message.setIntProperty("COMBAT_LEVEL",order);
                if (order==3){
//                    message.setJMSPriority(9);
//                    message.setText("中央军委一号令：全民皆兵");
                }else if(order==2){
//                    message.setJMSPriority(5);
//                    message.setText("国防部命令：福建省二级战备，准备收复台湾");
                }else {
//                    message.setJMSPriority(1);
//                    message.setText("绥德县武装部命令：民兵在村后头集合");
                }
                message.setText("收到战略情报");
                outposts.sender.send(message);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
